//
//  IntergralRecordListViewController.m
//  YYPHSpeedJump
//
//  Created by sujiang on 16/2/3.
//  Copyright © 2016年 sujiang. All rights reserved.
//

#import "IntergralRecordListViewController.h"
#import "RecordList.h"
#import "RecordModel.h"

@interface IntergralRecordListViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSInteger page;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation IntergralRecordListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    page = 1;
    self.view.backgroundColor = kColors(231, 231, 231, 1);
    self.title = @"兑换记录";
    [self addSubViews];
    [self requestData];
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
    
}


- (void)addSubViews
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"RecordList" bundle:nil] forCellReuseIdentifier:@"recordListCell"];
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
}


- (void)requestData
{
   // /user.do?cmd=integral_record&u=65&page=1
   // /user.do?cmd=exchange_record&u=65&page=1
    
    NSString *url = [NSString stringWithFormat:@"%@/user.do?cmd=%@", kBaseUrl, self.recordType];
    NSDictionary *dict = @{@"u":[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"], @"page":[NSNumber numberWithInteger:page]};
    [NetworkHandle getUpDataWithUrl:url class:NSClassFromString(@"RecordModel") dictionary:dict firstKey:@"data" secondKey:@"list" success:^(NSArray *array) {
        if (page == 1) {
            [self.dataArray removeAllObjects];
        }
        [self.dataArray addObjectsFromArray:array];
        if (self.dataArray.count >= 10 && self.tableView.footer == nil) {
            self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
        }
        
        [self.tableView.footer endRefreshing];
        [self.tableView.header endRefreshing];
        [self.tableView reloadData];
    }];
}

#pragma mark -- 下拉刷新加载
- (void)headerRefresh
{
    page = 1;
    [self requestData];
}

- (void)footerRefresh
{
    page++;
    [self requestData];
}


#pragma mark -- tableView代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataArray.count > 0) {
        return self.dataArray.count;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 53;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecordList *cell = [tableView dequeueReusableCellWithIdentifier:@"recordListCell" forIndexPath:indexPath];
    RecordModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
